package jp.kiroru_inc.rs_hello_world;

import jp.kiroru_inc.rs_hello_world.R;
import jp.kiroru_inc.rs_hello_world.ScriptC_HelloWorldScript;

import android.content.Context;
import android.content.res.Resources;
import android.renderscript.Allocation;
import android.renderscript.Float2;
import android.renderscript.Font;
import android.renderscript.RenderScriptGL;

/**
 * @author Selphy
 */
public class HelloWorldScript {
	private ScriptC_HelloWorldScript script;
	private ScriptField_RS_Text texts;
	private Font font;
	
	/**
	 * 色のクラスです。
	 * @author selphy
	 */
	private static class ColorRGB {
		float red, green, blue;
		private ColorRGB(float r, float g, float b) {
			red = r;
			green = g;
			blue = b;
		}
	}
	private final ColorRGB[] COLORS = new ColorRGB[]{new ColorRGB(1, 0, 0), new ColorRGB(0, 1, 0), new ColorRGB(0, 0, 1)};
	
	protected HelloWorldScript(RenderScriptGL rs, final Context c, Resources res) {
		script = new ScriptC_HelloWorldScript(rs, res, R.raw.helloworldscript);
		
		// -------------- 構造体の初期化 ------------------
		int length = COLORS.length;
		texts = new ScriptField_RS_Text(rs, length, Allocation.USAGE_SCRIPT);
		for (int i=0; i<length; i++) {
			ScriptField_RS_Text.Item item = new ScriptField_RS_Text.Item();
			item.text = Allocation.createFromString(rs, "HelloWorld", Allocation.USAGE_SCRIPT);
			ColorRGB color = COLORS[i];
			item.red = color.red;
			item.green = color.green;
			item.blue = color.blue;
			
			// 容器(.rsで定義した*gTexts)にアイテムを格納する感覚です。
			texts.set(item, i, true);
		}
		script.bind_gTexts(texts);
		
		font = Font.create(rs, res, "fantasy", Font.Style.NORMAL, 20);
		script.set_gFont(font);
		
		rs.bindRootScript(script);
	}
	
	protected void touchEvent(float x, float y) {
		script.invoke_setTouchPosition(new Float2(x, y));
	}
}